﻿using System;
using System.Net;
using System.IO;
using System.Linq;
namespace Reader
{
    public class ReaderPath
    {
        public static readonly string XmlExtractorFile = @"Resource/XmlExtractor.config";
        public static readonly string ObjectExtractorFile = @"Resource/ObjectExtractor.config";
        static ReaderPath()
        {
            if (App.Current.IsRunningOutOfBrowser)
            {
                SitesDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "SReader", "Sites");
                CacheDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "SReader", "Cache");
                SReaderDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "SReader");
                LocalFile = Path.Combine(ReaderPath.SitesDirectory, "loaclbook.xml");
                FavoriteFile = Path.Combine(ReaderPath.SitesDirectory, "favoritebook.xml");
            }
        }
        internal static readonly string SitesDirectory;
        internal static readonly string CacheDirectory;
        internal static readonly string SReaderDirectory;
        public static readonly string LocalFile;
        public static readonly string FavoriteFile;
        //internal static readonly string SitesDirectory = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "SReader", "Sites");
        //internal static readonly string CacheDirectory = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "SReader", "Cache");
        //internal static readonly string SReaderDirectory = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "SReader");
        //public static readonly string LocalFile = Path.Combine(ReaderPath.SitesDirectory, "loaclbook.xml");
        //public static readonly string FavoriteFile = Path.Combine(ReaderPath.SitesDirectory, "favoritebook.xml");
        /// <summary>
        /// 如果不存在站点路径，创建路径
        /// www.***.***
        /// </summary>
        /// <param name="uri"></param>
        /// <returns></returns>
        public static string EnsureSiteDirectory(string uri)
        {
           //string[] seg = new Uri(uri).Host.Split('.');
            
            if (!Directory.Exists(System.IO.Path.Combine(SitesDirectory, new Uri(uri).Host)))
            {
                Directory.CreateDirectory(System.IO.Path.Combine(SitesDirectory, new Uri(uri).Host));
            }
            return System.IO.Path.Combine(SitesDirectory, new Uri(uri).Host);
        }
        /// <summary>
        ///如果不存在创建路径
        /// </summary>
        /// <param name="path"></param>
        public static void EnsureDirectory(string path)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }
        
        public static string ConvertToWinPath(string uri)
        {
            return Uri.EscapeDataString(uri);
        }
        public static string ConvertToUri(string winPath)
        {
            return Uri.UnescapeDataString(winPath);
        }
        public static string GetSiteFileName(string uri)
        {
            string bookPath = EnsureSiteDirectory(uri);
            string fileLoc = System.IO.Path.Combine(bookPath, ConvertToWinPath(uri));
            return fileLoc;
        }
        //public static string GetSiteDirectory(string uri)
        //{
        //    //string siteDirectory = Path.Combine(EnsureSiteDirectory(uri));
        //   return EnsureSiteDirectory(uri);
        //}
        /// <summary>
        /// 读取本地文件
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="siteFileName"></param>
        /// <returns></returns>
        internal static bool TryGetSiteFileName(string uri, out string siteFileName)
        {
            siteFileName = GetSiteFileName(uri);
            return File.Exists(siteFileName);
        }
        /// <summary>
        /// 包括缓存与本地下载文件
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="localFileName"></param>
        /// <returns></returns>
        //internal static bool TryGetLoacalFileName(string uri, out string localFileName)
        //{
        //    //return (ReaderPath.TryGetCacheFileName(uri, out localFileName) || ReaderPath.TryGetSiteFileName(uri, out localFileName));
        //}
        /// <summary>
        /// 不能直接读取cache文件夹，禁止使用
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="cacheFileName"></param>
        /// <returns></returns>
        //internal static bool TryGetCacheFileName(string uri, out string cacheFileName)
        //{
        //    cacheFileName =Path.Combine(CacheDirectory,ConvertToWinPath(uri));
        //    return File.Exists(cacheFileName);
        //}
    }
}
